﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MathematicallySafe.HeadCounter.Common;
using MathematicallySafe.HeadCounter.ViewModels;
using StructureMap;

namespace MathematicallySafe.HeadCounter.Views
{
    /// <summary>
    /// Interaction logic for RaidersView.xaml
    /// </summary>
    public partial class RaidersView : UserControl, IDisposable
    {
        private Guid _toolbarContentId;
        private IActivatableContentContainer _contentContainer = ObjectFactory.GetInstance<IActivatableContentContainer>();

        public RaidersView()
        {
            InitializeComponent();
        }

        private void RaidersViewInitialized(object sender, EventArgs e)
        {
            ViewModel = new RaidersViewModel();
            ViewModel.Initialise();
            CreateInjectableContent();
        }

        void RaidersView_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            var isVisible = (bool) e.NewValue;
            var wasVisible = (bool) e.OldValue;

            if (isVisible)
            {
                _contentContainer.ActivateContent(_toolbarContentId);
            }
            else if (wasVisible)
            {
                _contentContainer.DeactivateContent();
            }
        }

        public RaidersViewModel ViewModel
        {
            get { return DataContext as RaidersViewModel; }
            set { DataContext = value; }
        }

        private void CreateInjectableContent()
        {
            _toolbarContentId = Guid.NewGuid();

            var content = new RaidersViewToolbar
            {
                ViewModel = ViewModel
            };

            IsVisibleChanged += RaidersView_IsVisibleChanged;
            _contentContainer.AddContent(new UserControlContent(_toolbarContentId, content));
        }

        public void Dispose()
        {
            IsVisibleChanged -= RaidersView_IsVisibleChanged;
        }
    }
}
